package com.qumitech.visualnovel.service.push.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.push.PushMessageAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.push.query.PushMessageQueryAdminDTO;
import com.qumitech.visualnovel.service.push.PushMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PushMessageServiceImpl implements PushMessageService {

    @Override
    public void saveMessagePushConfig(PushMessageAdminDTO adminDTO) {
        String url = UrlConstant.SERVER_URL + PUSH_URL;
        String rspBody = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(adminDTO), UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public BaseRsp listMessagePushConfig(PushMessageQueryAdminDTO queryAdminDTO) {
        String url = UrlConstant.SERVER_URL + PUSH_URL;
        Map<String, String> paramMap = new HashMap<>();
        if(StringUtils.isNotBlank(queryAdminDTO.getMessageId())){
            paramMap.put("message_id", queryAdminDTO.getMessageId());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getTitle())){
            paramMap.put("title", queryAdminDTO.getTitle());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getStartTime())){
            paramMap.put("start_time", queryAdminDTO.getStartTime());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getEndTime())){
            paramMap.put("end_time", queryAdminDTO.getEndTime());
        }
        if(queryAdminDTO.getLimit() != null){
            paramMap.put("limit", String.valueOf(queryAdminDTO.getLimit()));
        }
        if(queryAdminDTO.getOffset() != null){
            paramMap.put("offset", String.valueOf(queryAdminDTO.getOffset()));
        }
        if(queryAdminDTO.getOrderBy() != null){
            paramMap.put("orderBy", queryAdminDTO.getOrderBy());
        }
        if(queryAdminDTO.getOrderType() != null){
            paramMap.put("orderType", queryAdminDTO.getOrderType());
        }
        String rspBody = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSONObject.parseObject(rspBody, BaseRsp.class);
    }

    @Override
    public BaseRsp rmMessagePushConfig(String messageId) {
        String url = UrlConstant.SERVER_URL + PUSH_URL + "/" + messageId;
        JSONObject req = new JSONObject();
        String rspBody = HttpInvokeUtil.httpDeleteWithEntity(url, JSONObject.toJSONString(req),  new HashMap<>(), UrlConstant.DefaultEncode);
        return JSONObject.parseObject(rspBody, BaseRsp.class);
    }

    @Override
    public BaseRsp updateMessagePushConfig(String messageId, PushMessageAdminDTO adminDTO) {
        String url = UrlConstant.SERVER_URL + PUSH_URL + "/" + messageId;
        JSONObject o = JSONObject.parseObject(JSON.toJSONString(adminDTO));
        String body = HttpInvokeUtil.httpPutJson(url, o, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
